package exercise

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/exercise"
	exerciseReq "github.com/flipped-aurora/gin-vue-admin/server/model/exercise/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ExercisesApi struct {
}

var exercisesService = service.ServiceGroupApp.ExerciseServiceGroup.ExercisesService

// CreateExercises 创建exercise表
// @Tags Exercises
// @Summary 创建exercise表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body exercise.Exercises true "创建exercise表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /exercises/createExercises [post]
func (exercisesApi *ExercisesApi) CreateExercises(c *gin.Context) {
	var exercises exercise.Exercises
	err := c.ShouldBindJSON(&exercises)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := exercisesService.CreateExercises(&exercises); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteExercises 删除exercise表
// @Tags Exercises
// @Summary 删除exercise表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body exercise.Exercises true "删除exercise表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /exercises/deleteExercises [delete]
func (exercisesApi *ExercisesApi) DeleteExercises(c *gin.Context) {
	var exercises exercise.Exercises
	err := c.ShouldBindJSON(&exercises)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := exercisesService.DeleteExercises(exercises); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteExercisesByIds 批量删除exercise表
// @Tags Exercises
// @Summary 批量删除exercise表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除exercise表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /exercises/deleteExercisesByIds [delete]
func (exercisesApi *ExercisesApi) DeleteExercisesByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := exercisesService.DeleteExercisesByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateExercises 更新exercise表
// @Tags Exercises
// @Summary 更新exercise表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body exercise.Exercises true "更新exercise表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /exercises/updateExercises [put]
func (exercisesApi *ExercisesApi) UpdateExercises(c *gin.Context) {
	var exercises exercise.Exercises2
	err := c.ShouldBindJSON(&exercises)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	//fmt.Println(exercises)
	userid := utils.GetUserID(c)
	exercises.UserID = int(userid)
	if err := exercisesService.UpdateExercises(exercises); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindExercises 用id查询exercise表
// @Tags Exercises
// @Summary 用id查询exercise表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query exercise.Exercises true "用id查询exercise表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exercises/findExercises [get]
func (exercisesApi *ExercisesApi) FindExercises(c *gin.Context) {
	var exercises exercise.Exercises
	err := c.ShouldBindQuery(&exercises)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reexercises, err := exercisesService.GetExercises(exercises.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reexercises": reexercises}, c)
	}
}

// GetExercisesList 分页获取exercise表列表
// @Tags Exercises
// @Summary 分页获取exercise表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query exerciseReq.ExercisesSearch true "分页获取exercise表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /exercises/getExercisesList [get]
func (exercisesApi *ExercisesApi) GetExercisesList(c *gin.Context) {
	var pageInfo exerciseReq.ExercisesSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userid := int(utils.GetUserID(c))
	pageInfo.Userid = &userid
	if list, total, err := exercisesService.GetExercisesInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
